﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeetCodeFly.studyPlan.programming.day04
{
    public static class DayFour
    {
        public static int ArraySign(int[] nums)
        {
            // 除暴力破解 负负得正
            bool bl = true;
            foreach (var k in nums)
            {
                if (k == 0)
                {
                    return 0;
                }
                if (k < 0)
                {
                    bl = !bl;
                }
            }
            return bl ? 1 : -1;
        }

        public static bool CanMakeArithmeticProgression(int[] arr)
        {
            bool bl = false;
            int temp = -1;
            // 暴力破解
            Array.Sort(arr);
            for (int i = 0; i < arr.Length - 1; i++)
            {
                if (arr.Length == 2)
                {
                    return true;
                }
                if (temp == -1)
                {
                    temp = arr[i] - arr[i + 1];
                    continue;
                }
                if (arr[i] - temp == arr[i + 1])
                {
                    bl = true;
                }
                else
                {
                    return false;
                }
            }
            return bl;
        }

        public static bool IsHappy(int n)
        {           
            // hashset 不可插入重复值，如果出现重复，则这不是一个欢乐数
            HashSet<int> set = new HashSet<int>();
            int bitSquareSum(int n)
            {
                int sum = 0;
                while (n > 0)
                {
                    int bit = n % 10;
                    sum += bit * bit;
                    n = n / 10;
                }
                return sum;
            }
            if (n == 1)
            {
                return true;
            }
            else
            {
                while (true)
                {
                    var a = bitSquareSum(n);
                    if (set.Add(a))
                    {                        
                        if (bitSquareSum(a) == 1)
                        {
                            return true;
                        }
                        n = bitSquareSum(a);
                    }
                    else
                    {
                        return false;
                    }
                }
            }           
        }

        public static bool AreAlmostEqual(string s1, string s2)
        {
            // 暴力破解
            int[] index = new int[3];
            int r = 0;
            bool bl = false;
            if (s1 == s2)
            {
                return true;
            }
            for (int i = 0; i < s1.Length; i++)
            {
                if (r <3)
                {
                    var s = s1[i] != s2[i] ? index[r++] = i : 1;
                }
            }
            if (index[2] == 0 && index[1] != 0)
            {
                return s1[index[0]] == s2[index[1]] && s1[index[1]]==s2[index[0]];
            }
            return bl;
        }
    }
}
